﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InfoTech.Core.Data;
using InfoTech.Core.Data.SqlClient;
using InfoTech.LanTranh.MucVu.Bussiness;
using Microsoft.Reporting.WebForms;

namespace GiaoXuLanTranh_MucVu.Reports.BiTich
{
    public partial class RuaToi : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Guid guid;
                Panel1.Visible = false;
                if (Guid.TryParse(Request.QueryString["id"], out guid))
                {
                    GIAODAN giaodan = new GIAODAN();
                    giaodan.MAGIAODAN = guid;
                    ClsRuaToi clsRuaToi = new ClsRuaToi();
                    giaodan.SelectbyId();
                    if (giaodan.IsPersistence)
                    {
                        clsRuaToi.GiaoDan = new List<GIAODAN> { giaodan };
                        SOBITICH sobitich = new SOBITICH();
                        sobitich.MABITICH = 1;
                        sobitich.MAGIAODAN = giaodan.MAGIAODAN;
                        clsRuaToi.SoBiTich = InfoTech.Core.Data.Receiver.TypeObject<SOBITICH>.SelectMany(sobitich);
                        Panel1.Visible = true;
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("GiaoDan",
                                                                                       clsRuaToi.GetGiaoDan()));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("BiTich", clsRuaToi.GetBiTich()));

                        if (clsRuaToi.SoBiTich.Count() > 0)
                            sobitich = clsRuaToi.SoBiTich.First();
                        if (sobitich.PRT_LINHMUC.IsPersistence)
                        {
                            sobitich.TENLINHMUC = sobitich.PRT_LINHMUC.GetFullName();
                            if (sobitich.PRT_LINHMUC.PRT_LINHMUC_CHUCVU.IsPersistence)
                                clsRuaToi.LinhMucBiTich_ChucVu = new[] { sobitich.PRT_LINHMUC.PRT_LINHMUC_CHUCVU };
                            else
                                clsRuaToi.LinhMucBiTich_ChucVu = new List<LINHMUC_CHUCVU>() { new LINHMUC_CHUCVU() { TENCHUCVU = "Linh Mục" } };
                        }
                        else
                        {
                            clsRuaToi.LinhMucBiTich_ChucVu = new List<LINHMUC_CHUCVU>() { new LINHMUC_CHUCVU() { TENCHUCVU = "Linh Mục" } };
                        }
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("LinhMucBiTich_ChucVu", clsRuaToi.GetLinhMucBiTich_ChucVu()));

                        if (Guid.TryParse(Request.QueryString["machaxu"], out guid))
                        {
                            LINHMUC linhmuc = new LINHMUC();
                            linhmuc.MACHAXU = guid;
                            linhmuc.SelectbyId();
                            clsRuaToi.LinhMucChanhXu = new[] { linhmuc };
                            if (linhmuc.PRT_LINHMUC_CHUCVU.IsPersistence)
                                clsRuaToi.LinhMucChanhXu_ChucVu = new[] { linhmuc.PRT_LINHMUC_CHUCVU };
                            else
                                clsRuaToi.LinhMucChanhXu_ChucVu = new List<LINHMUC_CHUCVU>() { new LINHMUC_CHUCVU() { TENCHUCVU = "Linh Mục" } };
                        }
                        if (Guid.TryParse(Request.QueryString["machadaidien"], out guid))
                        {
                            LINHMUC linhmuc = new LINHMUC();
                            linhmuc.MACHAXU = guid;
                            linhmuc.SelectbyId();
                            clsRuaToi.LinhMucDaiDien = new[] { linhmuc };
                            if (linhmuc.PRT_LINHMUC_CHUCVU.IsPersistence)
                                clsRuaToi.LinhMucDaiDien_ChucVu = new[] { linhmuc.PRT_LINHMUC_CHUCVU };
                            else
                                clsRuaToi.LinhMucDaiDien_ChucVu = new List<LINHMUC_CHUCVU>() { new LINHMUC_CHUCVU() { TENCHUCVU = "Linh Mục" } };
                        }
                        //ReportViewer1.LocalReport.Refresh();
                        CONFIGURATION configuration_GIAOPHAN = new CONFIGURATION();
                        configuration_GIAOPHAN.ID = CONFIGURATION.DEFAULT_GIAOPHAN;
                        configuration_GIAOPHAN.Select();
                        if (configuration_GIAOPHAN.IsPersistence)
                        {
                            if (Guid.TryParse(configuration_GIAOPHAN.VALUE, out guid))
                            {
                                GIAOPHAN giaophan = new GIAOPHAN() { MAGIAOPHAN = guid };
                                giaophan.SelectbyId();
                                if (giaophan.IsPersistence)
                                    clsRuaToi.GiaoPhan = new[] { giaophan };
                            }
                        }

                        CONFIGURATION configuration_GIAOHAT = new CONFIGURATION();
                        configuration_GIAOHAT.ID = CONFIGURATION.DEFAULT_GIAOHAT;
                        configuration_GIAOHAT.Select();
                        if (configuration_GIAOHAT.IsPersistence)
                        {
                            if (Guid.TryParse(configuration_GIAOHAT.VALUE, out guid))
                            {
                                GIAOHAT giaohat = new GIAOHAT() { MAGIAOHAT = guid };
                                giaohat.SelectbyId();
                                if (giaohat.IsPersistence)
                                    clsRuaToi.GiaoHat = new[] { giaohat };
                            }
                        }

                        CONFIGURATION configuration_GIAOXU = new CONFIGURATION();
                        configuration_GIAOXU.ID = CONFIGURATION.DEFAULT_GIAOXU;
                        configuration_GIAOXU.Select();
                        if (configuration_GIAOXU.IsPersistence)
                        {
                            if (Guid.TryParse(configuration_GIAOXU.VALUE, out guid))
                            {
                                GIAOXU giaoxu = new GIAOXU() { MAGIAOXU = guid };
                                giaoxu.SelectbyId();
                                if (giaoxu.IsPersistence)
                                    clsRuaToi.GiaoXu = new[] { giaoxu };
                            }
                        }

                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("LinhMucChanhXu", clsRuaToi.GetLinhMucChanhXu()));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("LinhMucDaiDien", clsRuaToi.GetLinhMucDaiDien()));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("GiaoPhan", clsRuaToi.GiaoPhan));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("GiaoHat", clsRuaToi.GiaoHat));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("GiaoXu", clsRuaToi.GiaoXu));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("LinhMucChanhXu_ChucVu", clsRuaToi.LinhMucChanhXu_ChucVu));
                        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("LinhMucDaiDien_ChucVu", clsRuaToi.LinhMucDaiDien_ChucVu));
                        HOGIADINH hogiadinh = null;
                        string diachi = "";
                        if (giaodan.REF_HOGIADINH.Count() > 0)
                        {
                            hogiadinh = giaodan.REF_HOGIADINH.FirstOrDefault();
                        }
                        else
                        {
                            if (giaodan.REF_HOGIADINH_GIAODAN.Count() > 0)
                            {
                                HOGIADINH_GIAODAN hogiadinhGiaodan = giaodan.REF_HOGIADINH_GIAODAN.FirstOrDefault(c => c.DACHUYENDI != true && c.THOIGIANKETTHUC == null);
                                if (hogiadinhGiaodan != null)
                                {
                                    if (hogiadinhGiaodan.PRT_HOGIADINH.IsPersistence)
                                        hogiadinh = hogiadinhGiaodan.PRT_HOGIADINH;
                                }
                            }
                        }
                        if (hogiadinh != null)
                        {
                            diachi = hogiadinh.DIACHI;
                            if (string.IsNullOrEmpty(diachi))
                            {
                                if (hogiadinh.PRT_XAPHUONG.IsPersistence)
                                    diachi = string.Format("{0} - {1}", diachi, hogiadinh.PRT_XAPHUONG.TENXAPHUONG);
                                if (hogiadinh.PRT_QUANHUYEN.IsPersistence)
                                    diachi = string.Format("{0} - {1}", diachi, hogiadinh.PRT_QUANHUYEN.TENQUANHUYEN);
                                if (hogiadinh.PRT_TINHTHANH.IsPersistence)
                                    diachi = string.Format("{0} - {1}", diachi, hogiadinh.PRT_TINHTHANH.TENTINHTHANH);
                                if (hogiadinh.PRT_QUOCGIA.IsPersistence)
                                    diachi = string.Format("{0} - {1}", diachi, hogiadinh.PRT_QUOCGIA.TENQUOCGIA);
                            }
                        }
                        else
                            diachi = giaodan.DIACHI;
                        ReportViewer1.LocalReport.SetParameters(new ReportParameter("DIACHI", diachi));
                        clsRuaToi.DisableRefreshType();
                    }
                }
                Panel2.Visible = !Panel1.Visible;
            }
        }

    }
}